﻿namespace EFS.RC.SupervisorCentral.Controls.Agents
{
	public partial class RibbonItem : System.Windows.Controls.UserControl
	{
		/// Enum eImageType
		public enum eImageType
		{
			None,
			OK,
			Warning
		}

		#region Locals
		/// The type
		private eImageType imageType = eImageType.None;
		#endregion

		public RibbonItem()
		{
			InitializeComponent();

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
			{
				// switch type
				SwitchType();
			}
		}

		#region Properties
		/// Property Title: Get, Set
		public System.String Title
		{
			get { return textBlockTitle.Text; }
			set { textBlockTitle.Text = value; }
		}

		/// Property Title: Get, Set
		public System.String Info
		{
			get { return textBlockInfo.Text; }
			set { textBlockInfo.Text = value; }
		}

		/// Property ImageType: Get, Set
		public eImageType ImageType
		{
			get
			{
				return imageType;
			}
			set
			{
				imageType = value;
				SwitchType();
			}
		}
		#endregion

		#region Utilities
		/// IsThresholdLimitsInRange
		private static System.Boolean IsThresholdLimitsInRange(
			System.Int64 value,
			EFS.RC.SupervisorCentral.Common.Config.Sections.Monitoring.Thresholds.Limits limits)
		{
			// must exist
			if (null != limits)
			{
				// must be in range
				if (limits.Left <= value && value <= limits.Right)
				{
					return true;
				}
			}

			// no
			return false;
		}

		/// SetThreshold
		public void SetThreshold(
			System.Int64 value,
			EFS.RC.SupervisorCentral.Common.Config.Sections.Monitoring.Thresholds.Threshold thresshold)
		{
			eImageType imgType = eImageType.None;
			if (null != thresshold)
			{
				if (IsThresholdLimitsInRange(value, thresshold.Warning))
				{
					imgType = eImageType.Warning;
				}
				else if (IsThresholdLimitsInRange(value, thresshold.OK))
				{
					imgType = eImageType.OK;
				}
			}

			// play sound if the case
			EFS.RC.SupervisorCentral.Common.Sound.CheckPlay(imageType, thresshold);

			// set image
			ImageType = imgType;
		}

		/// Switches the type
		private void SwitchType()
		{
			// set defaults
			System.Windows.Media.Brush infoTextBrush = System.Windows.Media.Brushes.Black;
			System.Windows.Visibility imageVisibility = System.Windows.Visibility.Collapsed;
			System.Windows.Media.ImageSource imageSource = null;
			
			// switch type
			switch (imageType)
			{
				case eImageType.OK:
					{
						infoTextBrush = System.Windows.Media.Brushes.Green;
						imageVisibility = System.Windows.Visibility.Visible;
						imageSource = EFS.Common.UI.Resources.ImageSource.Cache.GetSource("Image_button_ok");
						break;
					}
				case eImageType.Warning:
					{
						infoTextBrush = System.Windows.Media.Brushes.Red;
						imageVisibility = System.Windows.Visibility.Visible;
						imageSource = EFS.Common.UI.Resources.ImageSource.Cache.GetSource("Image_messagebox_warning");
						break;
					}
				default:
					{
						break;
					}
			}

			// set
			textBlockInfo.Foreground = infoTextBrush;
			image.Source = imageSource;
			image.Visibility = imageVisibility;
		}
		#endregion
	}

}